package com.shuwang.goods.controller;

import com.shuwang.goods.domain.GoodsCategoryRequest;
import com.shuwang.goods.exception.ConflictException;
import com.shuwang.goods.exception.ForbiddenException;
import com.shuwang.goods.exception.NotFoundException;
import com.shuwang.goods.mapper.GoodsCategoryMapper;
import com.shuwang.goods.model.GoodsCategory;
import com.shuwang.goods.model.GoodsCategoryExample;
import com.shuwang.goods.model.GoodsCategoryRelative;
import com.shuwang.goods.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/5.
 */
@RestController
@RequestMapping(value = "goods")
@Api(value = "goods_category",description = "商品分类信息")
public class GoodsCategoryController {

        protected final Logger log = LoggerFactory.getLogger(getClass());

        @Autowired
        private GoodsCategoryMapper goodsCategoryMapper;

        @RequestMapping(value = "categoryInfo", method = RequestMethod.POST)
        @ApiOperation(value = "录入商品分类信息")
        public GoodsCategory createGoodsCategory(@ApiParam(value = "商品分类信息实体类") @Valid @RequestBody GoodsCategoryRequest request) {

            GoodsCategory goodsCategory = new GoodsCategory();
            GoodsCategoryExample goodsCategoryExample =new GoodsCategoryExample();
            goodsCategoryExample.createCriteria().andCategoryNameEqualTo(request.getCategoryName()).andDescriptionEqualTo(request.getDescription()).andSeqEqualTo(request.getSeq());
            if(!goodsCategoryMapper.selectByExample(goodsCategoryExample).isEmpty()){
                log.debug("该商品分类信息已存在");
                throw new ConflictException("该商品分类信息已存在");
            }

            goodsCategory.setCreateTime(new Date());
            Long categoryId = IDBuilder.CreateAnyId("112");
            goodsCategory.setCategoryId(categoryId);
            BeanUtils.copyProperties(request,goodsCategory);
            goodsCategoryMapper.insertSelective(goodsCategory);

            GoodsCategory success = goodsCategoryMapper.selectByPrimaryKey(categoryId);
            if(success == null ){
                log.debug("商品分类信息入库失败");
                throw new NotFoundException("商品分类信息入库失败");
            }
            return success;
        }

        @RequestMapping(value = "category/modifycategoryId/{categoryId}",method = {RequestMethod.POST,RequestMethod.PUT})
        @ApiOperation(value = "根据categoryId修改商品分类信息",response = GoodsCategory.class)
        public GoodsCategory reviseGoodsCategoryController(
                @ApiParam(value = "请输入categoryId") @PathVariable Long categoryId,
                @ApiParam(value = "请输入商品分类信息", required = true) @RequestBody @Valid GoodsCategoryRequest request) {

            if( goodsCategoryMapper.selectByPrimaryKey(categoryId) == null ){
                log.debug("该商品分类信息不存在");
                throw new NotFoundException("该商品分类信息不存在");
            }
            GoodsCategory goodsCategory = new GoodsCategory();
            goodsCategory.setCreateTime(new Date());
            goodsCategory.setCategoryId(categoryId);
            BeanUtils.copyProperties(request,goodsCategory);

            goodsCategoryMapper.updateByPrimaryKey(goodsCategory);
            if(goodsCategory ==null){
                log.debug("修改商品分类信息失败");
                throw new NotFoundException("修改商品分类信息失败");
            }
            return goodsCategoryMapper.selectByPrimaryKey(categoryId) ;
        }

        @RequestMapping(value = "category/getcategoryId/{categoryId}",method = {RequestMethod.GET})
        @ApiOperation(value = "根据categoryId查询商品分类信息", httpMethod = "", response = GoodsCategory.class)
        public List<GoodsCategory> getGoodsCategoryByCategoryIdController(@ApiParam(value = "请输入categoryId") @PathVariable Long categoryId ) {

            GoodsCategoryExample goodsCategoryExample =new GoodsCategoryExample();
            goodsCategoryExample.createCriteria().andCategoryIdEqualTo(categoryId);
            List<GoodsCategory> goodsCategory =goodsCategoryMapper.selectByExample(goodsCategoryExample);
            if( goodsCategory == null || goodsCategory.isEmpty()){
                log.debug("该商品分类信息不存在");
                throw new NotFoundException("该商品分类信息不存在");
            }
            return goodsCategory;
        }

        @RequestMapping(value = "category/getName/{categoryName}",method = {RequestMethod.GET})
        @ApiOperation(value = "根据categoryName查询商品属性信息", httpMethod = "", response = GoodsCategory.class)
        public List<GoodsCategory> getGoodsCategoryByCategoryNameController(@ApiParam(value = "请输入categoryName") @PathVariable String categoryName ) {

            GoodsCategoryExample goodsCategoryExample =new GoodsCategoryExample();
            goodsCategoryExample.createCriteria().andCategoryNameEqualTo(categoryName);
            List<GoodsCategory> goodsCategory =goodsCategoryMapper.selectByExample(goodsCategoryExample);
            if( goodsCategory == null|| goodsCategory.isEmpty() ){
                log.debug("该商品分类信息不存在");
                throw new NotFoundException("该商品分类信息不存在");
            }
            return goodsCategory;
        }

        @RequestMapping(value = "category/deleteCategoryId/{categoryId}",method = {RequestMethod.DELETE})
        @ApiOperation(value = "根据skuId删除商品属性信息", httpMethod = "DELETE", response = GoodsCategory.class)
        public List<GoodsCategory> deleteGoodsCategoryByCategoryIdController(@ApiParam(value = "请输入categoryId") @PathVariable Long categoryId ) {

            GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
            goodsCategoryExample.createCriteria().andCategoryIdEqualTo(categoryId);
            List<GoodsCategory> goodsCategory =goodsCategoryMapper.selectByExample(goodsCategoryExample);
            if (goodsCategory.isEmpty()) {
                log.debug("该商品分类信息不存在");
                throw new NotFoundException("该商品分类信息不存在");
            }
            goodsCategoryMapper.deleteByExample(goodsCategoryExample);

            if (!goodsCategoryMapper.selectByExample(goodsCategoryExample).isEmpty()) {
                log.debug("该商品属性信息删除失败");
                throw new ForbiddenException("该商品属性信息删除失败");

            }
            return goodsCategory;
        }


}
